package com.ruoyi.web.controller.estate;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.estate.Advertisement;
import com.ruoyi.system.service.estate.IAdvertisementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 广告管理Controller
 */
@RestController
@RequestMapping("/estate/advertisement")
public class AdvertisementController extends BaseController {
    @Autowired
    private IAdvertisementService advertisementService;

    /**
     * 查询广告列表
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:list')")
    @GetMapping("/list")
    public TableDataInfo list(Advertisement advertisement) {
        startPage();
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        return getDataTable(list);
    }

    /**
     * 导出广告列表
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:export')")
    @Log(title = "广告管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Advertisement advertisement) {
        List<Advertisement> list = advertisementService.selectAdvertisementList(advertisement);
        ExcelUtil<Advertisement> util = new ExcelUtil<Advertisement>(Advertisement.class);
        return util.exportExcel(list, "广告数据");
    }

    /**
     * 获取广告详细信息
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:query')")
    @GetMapping(value = "/{adId}")
    public AjaxResult getInfo(@PathVariable("adId") Long adId) {
        return success(advertisementService.selectAdvertisementById(adId));
    }

    /**
     * 新增广告
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:add')")
    @Log(title = "广告管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Advertisement advertisement) {
        // 处理图片URL，确保格式正确
        if (advertisement.getAdImage() != null && advertisement.getAdImage().startsWith("/profile")) {
            // 如果是相对路径，不做处理，保持原样
            // 系统会根据配置自动处理URL
        }
        return toAjax(advertisementService.insertAdvertisement(advertisement));
    }

    /**
     * 修改广告
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:edit')")
    @Log(title = "广告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Advertisement advertisement) {
        // 处理图片URL，确保格式正确
        if (advertisement.getAdImage() != null && advertisement.getAdImage().startsWith("http://localhost:8080/profile")) {
            // 如果是完整URL，去掉前缀，只保留相对路径
            advertisement.setAdImage(advertisement.getAdImage().replace("http://localhost:8080", ""));
        }
        return toAjax(advertisementService.updateAdvertisement(advertisement));
    }

    /**
     * 删除广告
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:remove')")
    @Log(title = "广告管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{adIds}")
    public AjaxResult remove(@PathVariable Long[] adIds) {
        return toAjax(advertisementService.deleteAdvertisementByIds(adIds));
    }
    
    /**
     * 更新广告点击次数
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:edit')")
    @Log(title = "广告管理", businessType = BusinessType.UPDATE)
    @PutMapping("/click/{adId}")
    public AjaxResult updateClickCount(@PathVariable("adId") Long adId) {
        return toAjax(advertisementService.updateClickCount(adId));
    }
    
    /**
     * 发布广告
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:edit')")
    @Log(title = "广告管理", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{adId}")
    public AjaxResult publish(@PathVariable("adId") Long adId) {
        return toAjax(advertisementService.publishAdvertisement(adId));
    }
    
    /**
     * 下架广告
     */
    @PreAuthorize("@ss.hasPermi('estate:advertisement:edit')")
    @Log(title = "广告管理", businessType = BusinessType.UPDATE)
    @PutMapping("/unpublish/{adId}")
    public AjaxResult unpublish(@PathVariable("adId") Long adId) {
        return toAjax(advertisementService.unpublishAdvertisement(adId));
    }
} 